<script setup>
import { ref, onMounted, watch } from 'vue';
import { ElMessage } from 'element-plus';
import { useDocumentStore } from '@/stores/document';
import '@wangeditor/editor/dist/css/style.css';
import { Editor, Toolbar } from '@wangeditor/editor-for-vue';

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  versionData: {
    type: Object,
    default: () => ({})
  }
});

const emit = defineEmits(['update:visible', 'back-to-list']);

const documentStore = useDocumentStore();
const loading = ref(false);
const versionDetail = ref(null);
const editorRef = ref(null);

// 编辑器配置
const editorConfig = {
  readOnly: true,
  placeholder: '文档内容为空'
};

// 工具栏配置 - 只读模式下为空
const toolbarConfig = {};

// 获取文档版本详情
const fetchVersionDetail = async () => {
  if (!props.versionData || !props.versionData.id) return;
  
  loading.value = true;
  try {
    const data = await documentStore.fetchDocumentVersionById(props.versionData.id);
    if (data) {
      versionDetail.value = data;
    } else {
      ElMessage.error('获取文档版本详情失败');
    }
  } catch (error) {
    ElMessage.error('获取文档版本详情失败');
    console.error('Failed to fetch document version detail:', error);
  } finally {
    loading.value = false;
  }
};

// 返回版本列表
const backToList = () => {
  emit('back-to-list');
};

// 关闭对话框
const closeDialog = () => {
  emit('update:visible', false);
};

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return '';
  const date = new Date(dateString);
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  });
};

// 监听对话框可见性变化
watch(
  () => props.visible,
  (newVal) => {
    if (newVal && props.versionData && props.versionData.id) {
      fetchVersionDetail();
    }
  }
);

// 监听版本数据变化
watch(
  () => props.versionData,
  (newVal) => {
    if (props.visible && newVal && newVal.id) {
      fetchVersionDetail();
    }
  },
  { deep: true }
);

// 组件卸载前清理
onMounted(() => {
  if (props.visible && props.versionData && props.versionData.id) {
    fetchVersionDetail();
  }
});
</script>

<template>
  <el-dialog
    title="文档版本详情"
    v-model="props.visible"
    @update:modelValue="emit('update:visible', $event)"
    width="80%"
    destroy-on-close
    class="version-view-dialog"
    top="5vh"
  >
    <div class="version-view-container">
      <div class="version-toolbar">
        <button @click="backToList" class="back-btn">
          <span class="icon">←</span> 返回版本列表
        </button>
        
        <div class="version-info" v-if="versionDetail">
          <span class="version-number">版本号: {{ versionDetail.version }}</span>
          <span class="version-date">创建时间: {{ formatDate(versionDetail.createTime) }}</span>
          <span class="version-creator" v-if="versionDetail.user">
            创建者: {{ versionDetail.user.userName }}
          </span>
        </div>
      </div>

      <div v-if="loading" class="loading-state">
        <el-skeleton :rows="10" animated />
      </div>
      
      <div v-else-if="!versionDetail" class="empty-state">
        <p>无法加载版本详情</p>
      </div>
      
      <div v-else class="document-content">
        <h1 class="document-title">{{ versionDetail.title || versionDetail.documentTitle || '无标题文档' }}</h1>
        
        <div v-if="versionDetail.remark" class="version-remark">
          <h3>版本备注:</h3>
          <p>{{ versionDetail.remark }}</p>
        </div>
        
        <div class="editor-container">
          <div class="editor-wrapper">
            <Editor
              v-model="versionDetail.content"
              :defaultConfig="editorConfig"
              mode="default"
              class="editor"
              @onCreated="editorRef = $event"
            />
          </div>
        </div>
      </div>
    </div>
  </el-dialog>
</template>

<style scoped>
.version-view-dialog :deep(.el-dialog__header) {
  padding: 20px 24px;
  border-bottom: 1px solid #ebeef5;
  margin-right: 0;
}

.version-view-dialog :deep(.el-dialog__title) {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.version-view-dialog :deep(.el-dialog__body) {
  padding: 24px;
}

.version-view-dialog :deep(.el-dialog__headerbtn) {
  top: 20px;
  right: 20px;
}

.version-view-container {
  min-height: 500px;
  display: flex;
  flex-direction: column;
}

.version-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #ebeef5;
  background-color: #f9f9f9;
  padding: 15px;
  border-radius: 4px;
}

.back-btn {
  display: flex;
  align-items: center;
  background: #f2f6fc;
  border: 1px solid #dcdfe6;
  color: #409eff;
  cursor: pointer;
  font-size: 14px;
  padding: 8px 16px;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.back-btn:hover {
  color: #66b1ff;
  background-color: #ecf5ff;
  border-color: #c6e2ff;
}

.back-btn .icon {
  margin-right: 5px;
  font-weight: bold;
}

.version-info {
  display: flex;
  gap: 20px;
  color: #606266;
  font-size: 14px;
  background-color: #f2f6fc;
  padding: 10px 15px;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.version-number, .version-date, .version-creator {
  display: flex;
  align-items: center;
}

.version-number::before {
  content: '📄';
  margin-right: 5px;
}

.version-date::before {
  content: '🕒';
  margin-right: 5px;
}

.version-creator::before {
  content: '👤';
  margin-right: 5px;
}

.loading-state,
.empty-state {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 300px;
}

.empty-state p {
  color: #909399;
  font-size: 14px;
}

.document-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  background-color: #fff;
  padding: 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.document-title {
  font-size: 24px;
  margin-bottom: 20px;
  color: #303133;
  font-weight: 500;
  padding-bottom: 15px;
  border-bottom: 1px dashed #ebeef5;
}

.version-remark {
  background-color: #f8f9fa;
  border-radius: 4px;
  padding: 15px;
  margin-bottom: 20px;
  border-left: 4px solid #409eff;
}

.version-remark h3 {
  font-size: 16px;
  margin-top: 0;
  margin-bottom: 8px;
  color: #606266;
}

.version-remark p {
  margin: 0;
  color: #303133;
  line-height: 1.6;
}

.editor-container {
  flex: 1;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.03);
}

.editor-wrapper {
  height: 500px;
  overflow-y: auto;
  background-color: #fff;
}

.editor {
  height: 100%;
}

.editor :deep(.w-e-text-container) {
  background-color: #fff;
}
</style>